import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { commonManageSearchParams, getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
import { getManageTabSwitch, getSearchList } from '../mconfig';
import { getBaseUrl } from '@/services/urlconst';
import moment from 'moment';

const baseUrl = getBaseUrl();

const warnManageService = (curRes: Record<string, any>) => {
  /** 获取数据字典 **/
  const loadMdicIds: string[] = [`${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`];

  /** 获取数据字典loadMdicMap */
  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /** 获取权限参数 */
  const queryTypeObj: Record<string, any> = getManageTabSwitch(curRes);

  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        timeType: 3, // 初始化一个默认日维度
        // sort: 'readTime',
        // order: 'desc',
        // dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };
      const requestURL = `${baseUrl}/assetStatistics/scrapAssetList/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    if (request.statisticalType) {
      request.timeType = request.statisticalType;
      delete request['statisticalType'];
    }

    if (request?.statisticalTime) {
      const [statisticalTimeStart, statisticalTimeEnd] = request.statisticalTime;
      if (request.timeType === 1) {
        request['statisticalTimeStart'] = moment(statisticalTimeStart).startOf('year').format('YYYY-MM-DD HH:mm:ss');
        request['statisticalTimeEnd'] = moment(statisticalTimeEnd).endOf('year').format('YYYY-MM-DD HH:mm:ss');
      } else if (request.timeType === 2) {
        request['statisticalTimeStart'] = moment(statisticalTimeStart).startOf('month').format('YYYY-MM-DD HH:mm:ss');
        request['statisticalTimeEnd'] = moment(statisticalTimeEnd).endOf('month').format('YYYY-MM-DD HH:mm:ss');
      } else if (request.timeType === 3) {
        request['statisticalTimeStart'] = moment(statisticalTimeStart).startOf('day').format('YYYY-MM-DD HH:mm:ss');
        request['statisticalTimeEnd'] = moment(statisticalTimeEnd).endOf('day').format('YYYY-MM-DD HH:mm:ss');
      }
      delete request['statistical'];
      delete request['statisticalTime'];
      delete request['statisticalTimeDept'];
      delete request['statisticalTimeUser'];
    }

    run({
      ...request,
    });
  };

  /** 搜索列表  */
  const searchList = useMemo(() => {
    return {
      searchList: getSearchList(loadMdicMapObj?.loadMapInfo),
    };
  }, [loadMdicMapObj?.loadMapInfo]);

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
    ...searchList,
  };
};

export { warnManageService };
